#this file just loads the monitor to the right place --
#the absolute address 128k,the monitor will load kernel
#and do something pre-load stuff
#so, this is just a simple file

.code16
.text

.set BASE,		0x7c00
.set STACK_ALIGN,	0xFFFF

.macro init_stack
movw $0x9000,%sp
.endm

.macro set_cursor_line cursor
movw \cursor, %ax
pushw %ax
call do_set_cursor_line
.endm

.macro show_msg msg_addr,msg_len
pushw \msg_addr
pushw $STACK_ALIGN
pushw \msg_len
call do_show_msg
.endm

.globl _start
_start:
		movw %cs,%ax
		movw %ax,%ss
		movw %ax,%es
		movw %ax,%ds
		
		# make all common registers clean
		xorl %eax,%eax
		xorl %ecx,%ecx
		xorl %edx,%edx
		xorl %ebx,%ebx
		xorl %edi,%edi
		xorl %esi,%esi
		xorl %ebp,%ebp

		# initialize the stack
		init_stack

		# clear the screen
		movw $0x0600,%ax
		movw $0,%bx
		movw $0,%cx
		movw $0x184F,%dx
		int $0x10

		# set the position of cursor
		set_cursor_line $0x0000
		show_msg $infor_os,$len_os

		set_cursor_line $0x0001
		show_msg $infor_copy,$len_copy

		set_cursor_line $0x0002
		show_msg $infor_load,$len_load

		# load the monitor
		movw $0x100,%ax
		movw %ax,%es
		movw $0x0000,%bx

		movw $0x0230,%ax
		movw $0x0002,%cx
		movw $0x0080,%dx
		int $0x13
to_monitor:
		pushw $0x100
		pushw $0x0000
		lret

##############################################################
#                                                            #
#               LOCAL FUNCTION                               #
#                                                            #
##############################################################


do_set_cursor_line:
		pushw %bp
		movw %sp,%bp

		movl $0x00000001,%edi
		andl $0x0000FFFF,%ebp

		movb $0x02,%ah
		movb $0x02,%bh
		movb $0x00,%dl
		movb %ss:(%ebp,%edi,4),%dh
		int $0x10

		popw %bp
		retw $0x02

do_show_msg:
		pushw %si
		movw %sp,%si

		movl $0x00000001,%edi
		andl $0x0000FFFF,%esi
		xorl %ecx,%ecx

		movw %ss:(%esi,%edi,4),%cx
		addw $BASE,%cx
		xorl %edi,%edi
		movw %ds:(%ecx,%edi,1),%cx

		incl %edi
		movw %ss:(%esi,%edi,8),%ax
		addw $BASE,%ax
		movw %ax,%bp
		movw $0x1301,%ax
		movw $0x000F,%bx
		int $0x10

		popw %si
		retw $0x06

infor_os:   .ascii	"nonstandard ver0.01\n"
len_os:  .word .-infor_os
infor_copy: .ascii	"copyright (c) 2013.5.20  All rights reserved.\n"
len_copy:  .word .-infor_copy
infor_load: .ascii	"loading monitor...\n"
len_load:  .word .-infor_load

.end

# EOF

